/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dataaccess;

import abstractmodel.IInfo;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jdo.*;

/**
 *
 * @author littlechild
 */
public class JDOmanager {
    PersistenceManagerFactory pmf;
    PersistenceManager pm;
    public JDOmanager() {
        try {
            Properties property = new Properties();
            property.load(new FileInputStream("database.properties"));
            property.setProperty("javax.jdo.option.ConnectionPassword", "ba;dkj}fua[{");
            pmf = JDOHelper.getPersistenceManagerFactory(property);
            pm=pmf.getPersistenceManager();
            System.out.println("pm initate ok");
        } catch (IOException ex) {
            System.out.println("database configure file not found");
            Logger.getLogger(JDOmanager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public boolean saveDetailInfo(IInfo info) {
        pm.currentTransaction().begin();
        Query q=pm.newQuery(info.getClass(),"id=="+info.getid());
        Collection result=(Collection)q.execute();
        if(!result.isEmpty()) {
            Iterator it=result.iterator();
            while(it.hasNext()) {
                pm.deletePersistent(it.next());
            }
        }
        pm.makePersistent(info) ;
        Object oid=pm.getObjectId(info);
        pm.currentTransaction().commit();
        
        pm.currentTransaction().begin();
        info=(IInfo)pm.getObjectById(oid);
        pm.makeTransient(info);
        pm.currentTransaction().commit();
        
        return true;
    }

    public boolean deleteDetailInfo(IInfo info) {
        pm.currentTransaction().begin();
        Query q=pm.newQuery(info.getClass(), "id=="+info.getid());
        Collection result=(Collection)q.execute();
        if(!result.isEmpty()) {
            pm.deletePersistent(result.iterator().next());
        }
        q.close(result);
        pm.currentTransaction().commit();
        return true;
    }

    public ArrayList<IInfo> search(String filter, Class info) {
        pm.currentTransaction().begin();
        Query q=pm.newQuery(model.DetailInfo.class, filter);
        Collection result=(Collection)q.execute();
        Iterator it=result.iterator();
        IInfo temp;
        while(it.hasNext()) {
            temp=(IInfo)it.next();
            pm.makeTransient(temp);
        }
        pm.currentTransaction().commit();
        
        ArrayList<IInfo> returnResult=new ArrayList<IInfo>();
        it=result.iterator();
        while(it.hasNext()) {
            temp=(IInfo)it.next();
            returnResult.add(temp);
        }
        
        pm.currentTransaction().begin();
        q.close(result);
        pm.currentTransaction().commit();
        return returnResult;
    }
    
    public void close() {
        if(pmf!=null)
            pmf.close();
        if(pm!=null)
            pm.close();
    }
}
